<?php

namespace app\index\model;
use think\Db;
use think\Model;

class TableModel extends Model
{
    public function lists()
    {
        $data = Db::table('table')
                    ->select();
        return $data;
    }

    public function sure()
    {
        $data = $_POST;
       // var_dump($data);die;
       // 
       if ($data['ymd']['month'] < 10) {
           $data['ymd']['month'] = '0'.$data['ymd']['month'];
       }
       if ($data['ymd']['day'] < 10) {
           $data['ymd']['day'] = '0'.$data['ymd']['day'];
       }

        $data['time'] = $data['ymd']['year'].'-'.$data['ymd']['month'].'-'.$data['ymd']['day'];
        unset($data['ymd']);
        $data['status'] = 2;
        // var_dump($data);die;
        // var_dump($data['time']);die;
        $time = date('Y-m-d,H:i');
        // var_dump($time);die;
        
        // 切割时间切出年月日
        $preg = '/(\d{4})-(\d{2})-(\d{2})/';
        preg_match( $preg , $data['time'] , $res );
        preg_match( $preg , $time , $res2 );
        // var_dump($res);
        // echo '<hr>';
        // var_dump($res2);die;

        // 用截取到的年份判断 用户设定年龄是否在 18 ~ 100岁 之间
        if( ($res[1] >= $res2[1]) ){
            if ($res[2] < $res2[2]) {
                echo "<script> alert('请输入正确月份');history.go(-1); </script>";die;
            }else{
                if ($res[2] == $res2[2]){
                    if (($res[3]) < $res2[3] + 1) {
                        echo "<script> alert('请至少提前一天预定');history.go(-1); </script>";die;
                    }
                }
                if ($res[2] > $res2[2]){
                        $data['time'] .= '/';
                        $data['time'] .= ($data['hmm']['hour'].':'.$data['hmm']['minute'].$data['hmm']['ampm']);
                        unset($data['hmm']);
                        // var_dump($data);die;

                        $res = Db::table('table')
                        ->field('id')
                        ->where(['count'=>$data['member'], 'status'=>1])
                        ->select();

                       // var_dump($res);die;

                        if($res){
                            $pre = Db::table('table')
                                        ->where('id',$res[0]['id'])
                                        ->update($data);
                           // var_dump($pre);die;

                            if($pre <= 0){
                                $pre = false;
                            }

                        }else{
                            $pre = "haven't table";
                        }

                        return $pre;
                }
            }
        }else{
            echo "<script> alert('请输入正确年份');history.go(-1); </script>";die;
        }


        
    }
}
















